package com.wss.module.market.ui.goods.detail.adapter;

import com.felipecsl.asymmetricgridview.AgvRecyclerViewAdapter;
import com.felipecsl.asymmetricgridview.AsymmetricItem;
import com.felipecsl.asymmetricgridview.BaseViewHolder;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.utils.ImageUtils;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.GoodsInfo;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

/**
 * Describe：商品详情 店主推荐 商品详情适配器
 * Created by 吴天强 on 2018/10/19.
 */
public class RecommendGoodsInfoAdapter extends AgvRecyclerViewAdapter<RecommendGoodsInfoAdapter.ViewHolder> {

    private Context context;
    private List<GoodsInfo> items;
    private OnListItemClickListener<GoodsInfo> listener;

    public RecommendGoodsInfoAdapter(Context context,
                                     List<GoodsInfo> items,
                                     OnListItemClickListener<GoodsInfo> listener) {

        this.context = context;
        this.items = items;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public GoodsInfo getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer, int i) {
        return new ViewHolder();
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.bind(items.get(i), i);
    }


    class ViewHolder extends BaseViewHolder {

        Image ivGoods;
        Text tvGoodsName;
        Text tvGoodsPrice;
        Text tvGoodsOldPrice;

        public ViewHolder() {

            super(LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_market_item_of_goods_recommend_list,
                            null,
                            false));
            ivGoods = (Image) getComponent().findComponentById(ResourceTable.Id_iv_goods);
            tvGoodsName = (Text) getComponent().findComponentById(ResourceTable.Id_tv_goods_name);
            tvGoodsPrice = (Text) getComponent().findComponentById(ResourceTable.Id_tv_goods_price);
            tvGoodsOldPrice = (Text) getComponent().findComponentById(ResourceTable.Id_tv_goods_old_price);
        }

        public void bind(GoodsInfo data, int position) {
            ImageUtils.loadImage(ivGoods, data.getGoodsMasterImg());
            tvGoodsName.setText(data.getGoodsName());
            tvGoodsPrice.setText(String.format("¥%s", data.getGoodsPrice()));
            tvGoodsOldPrice.setText(String.format("¥%s", data.getGoodsOldPrice()));

            ivGoods.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    listener.onItemClick(data, position);
                }
            });
        }
    }
}
